public class HashBuck<K,V> {
    // key-value 模型
        private static class Node<K,V> {
            private K key;
            private V value;
            Node next;

            public Node(K key, V value) {
                this.key = key;
                this.value = value;
            }
        }


        private Node[]  array;
        private int size;   // 当前的数据个数
        private static final double LOAD_FACTOR = 0.75;
        private static final int DEFAULT_SIZE = 8;//默认桶的大小

        public V put(K key, V value) {
            // write code here
            int index = key.hashCode()%array.length;
            Node cur = array[index];

            while(cur != null){
                if(cur.key.equals(key)){
                    cur.value = value;
                    return value;
                }
                cur = cur.next;
            }

            if(loadFactor() > LOAD_FACTOR){
                resize();
            }

            cur = array[index];
            Node<K,V> newNode = new Node<>(key,value);
            newNode.next = cur;
            array[index] = newNode;
            size++;
            return value;
        }

        private void resize() {
            // write code here
            Node[] newArray = new Node[2*array.length];

            for (Node node : array) {
                Node tmp = node;
                while(tmp != null){
                    Node tmpNext = tmp.next;
                    int newIndex = tmp.key.hashCode() % array.length;
                    Node cur = newArray[newIndex];

                    tmp.next = cur;
                    newArray[newIndex] = tmp;
                    tmp = tmpNext;
                }
            }
            array = newArray;
        }

        private double loadFactor() {
            return size * 1.0 / array.length;
        }

        public HashBuck() {
             this.array = new Node[DEFAULT_SIZE];
         }

        public V get(K key) {
            // write code here
            int index = key.hashCode() % array.length;
            Node cur = array[index];

            while (cur != null){
                if(cur.key.equals(key)){
                    return (V) cur.value;
                }
                cur = cur.next;
            }

            return null;
        }
}
